# SDCC=$(shell pwd)/tools/pbccv2-bin-win32-cygwin-2011-10-24/bin/sdcc.exe
SDCC=$(shell pwd)/tools/pbccv2.1/bin/sdcc.exe

# SDCC=/cygdrive/g/Stu/13_FPGA_Stu/07_cbb-repo/pbcc/pbccv2-src-20110901/sdcc3/pbcc/bin/sdcc.exe
# SDCC=/cygdrive/g/Stu/13_FPGA_Stu/07_cbb-repo/sdcc-kcpsm3w32/pbccv2-src-20110901/sdcc3/pbcc/bin/sdcc.exe
# SDCC=/cygdrive/g/Stu/13_FPGA_Stu/07_cbb-repo/pbccv2.1/pbccv2-src-20111024/sdcc3/pbcc/bin/sdcc.exe

SDCC_INC=$(shell pwd)/tools/pbccv2-bin-win32-cygwin-2011-10-24/include/pblaze

# 仿真 psm 下的状态机，并打包发布文件到build文件夹
psm_%:
	mkdir -p build 
	cd build && opbasm --m4 -3 -r ../psm/$@/main.psm 
	cd build && cp main.mem $@.hex  && sed -i '1d' $@.hex 
	iverilog -o build/tb.vvp  -y ./rtl -y ./psm/$@ psm/$@/tb.v  
	@echo " --- sim ---"
	cd build && vvp tb.vvp 
	@echo " -----------"
	@-cd build && grep -A6 "Last occupied address" main.log
	@cp -r psm/$@ build && cp build/$@.hex build/$@  


# 基于PBCCV2编译cpsm程序，然后仿真
cpsm_%:
	@mkdir -p build
	@cd build && $(SDCC) ../cpsm/$@/$@.c -mpblaze -S -I$(SDCC_INC) -I$(shell pwd)/../cpsm/$@ -o $@.asm 
	# @cd build && sed -i 's/_misr/misr/g' $@.asm
	@cd build && opbasm --m4 -3 -r $@.asm
	@cd build && cp $@.mem $@.hex  && sed -i '1d' $@.hex 
	iverilog -o build/tb.vvp  -y ./rtl -y ./cpsm/$@ cpsm/$@/tb.v  
	@echo " --- sim ---"
	@cd build && vvp tb.vvp 
	@echo " -----------"
	@-cd build && grep -A6 "Last occupied address" $@.log
	@cp -r cpsm/$@ build && cp build/$@.hex build/$@   